import qs from "qs"
const baseUrl = 'https://xcrj.bigchun.com';

const httpRequest = (url, method = "get", data) => {
	let httpDefaultOpts = {
		url: baseUrl + url,
		data: data,
		method: method,
		header: method == 'get' ? {
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/x-www-form-urlencoded"
		} : {
			'X-Requested-With': 'XMLHttpRequest',
			// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
			"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
		},
		// dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.showLoading({
			title: '加载中...',
			icon: 'none'
		})
		uni.request(httpDefaultOpts).then(
			(res) => {
				uni.hideLoading()
				// console.log(res)
				// if(res[1].data.code == 9999 || res[1].data.code == 401){
				// 	uni.showToast({
				// 		title:res[1].data.msg,
				// 		icon:"none"
				// 	})
				// 	uni.navigateTo({
				// 		url:"/pages/enter/login"
				// 	})
				// 	reject(res[1])
				// }
				if (res[1].data.code !== 1) {
					uni.showToast({
						title: res[1].data.msg,
						icon: "none"
					})
					reject(res[1].data)
				}
				// console.log(JSON.stringify(res))
				// resolve(res[1])
				resolve(res[1].data)
			}
		).catch(
			(response) => {
				uni.hideLoading()
				reject(response)
			}
		)
	})
	return promise
};
const get = (url, data) => {
	return httpRequest(url, 'get', data)
}
const post = (url, data) => {
	return httpRequest(url, 'post', data)
}


export default {
	baseUrl,
	post,
	get,
}
